package com.glory.eapequipmentmonitor.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.eapequipmentmonitor.R;
import com.glory.eapequipmentmonitor.adapter.ThingListAdapter;
import com.glory.eapequipmentmonitor.base.Constant;
import com.glory.eapequipmentmonitor.bean.ThingBean;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hx
 * @date 2019/8/14
 * @desc
 */
public class StartWorkActivity extends FreeUI_BaseActivity {

    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.rbLot)
    RadioButton rbLot;
    @BindView(R.id.rbWork)
    RadioButton rbWork;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvLotNum)
    TextView tvLotNum;
    @BindView(R.id.llLotNum)
    LinearLayout llLotNum;
    @BindView(R.id.tvLotTitle)
    TextView tvLotTitle;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.ivAddCarryId)
    ImageView ivAddCarryId;
    @BindView(R.id.tvCarryId)
    TextView tvCarryId;
    @BindView(R.id.rlCarryId)
    RelativeLayout rlCarryId;
    @BindView(R.id.etMainEqp)
    FreeUI_ClearEditText etMainEqp;
    @BindView(R.id.ivMainArrow)
    ImageView ivMainArrow;
    @BindView(R.id.llMainEqp)
    LinearLayout llMainEqp;
    @BindView(R.id.llMainEqpParent)
    LinearLayout llMainEqpParent;
    @BindView(R.id.etChildEqp)
    FreeUI_ClearEditText etChildEqp;
    @BindView(R.id.ivChildArrow)
    ImageView ivChildArrow;
    @BindView(R.id.llChildEqp)
    LinearLayout llChildEqp;
    @BindView(R.id.llChildEqpParent)
    LinearLayout llChildEqpParent;
    @BindView(R.id.tvProvider)
    TextView tvProvider;
    @BindView(R.id.ivProviderArrow)
    ImageView ivProviderArrow;
    @BindView(R.id.llProviderSelect)
    LinearLayout llProviderSelect;
    @BindView(R.id.llProviderItem)
    LinearLayout llProviderItem;
    @BindView(R.id.tvGoodsPlace)
    TextView tvGoodsPlace;
    @BindView(R.id.etGoodsPlace)
    FreeUI_ClearEditText etGoodsPlace;
    @BindView(R.id.llGoodsPlace)
    LinearLayout llGoodsPlace;
    @BindView(R.id.tvGoodsShelves)
    TextView tvGoodsShelves;
    @BindView(R.id.etGoodsShelves)
    FreeUI_ClearEditText etGoodsShelves;
    @BindView(R.id.llGoodsShelves)
    LinearLayout llGoodsShelves;
    @BindView(R.id.ivAdd)
    ImageView ivAdd;
    @BindView(R.id.rvThingList)
    RecyclerView rvThingList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private FreeUI_GeneralFragmentDialog addCarryIdDialog;
    private List<ThingBean> mLots = new ArrayList<>();
    private ThingListAdapter thingListAdapter;

    @OnClick({R.id.ivAdd, R.id.rlCarryId, R.id.rbLot, R.id.rbWork})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivAdd:
                final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
                View view = LayoutInflater.from(StartWorkActivity.this).inflate(R.layout.dialog_scan_thing, null);
                TextView tvCancel = view.findViewById(R.id.tvCancel);
                final FreeUI_ClearEditText etThingCode = view.findViewById(R.id.etThingCode);
                final FreeUI_ClearEditText etThingId = view.findViewById(R.id.etThingId);
                etThingCode.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (start == 0 && before == 0 && count > 1) {
                            if (etThingId.getText().toString().trim().length() > 0) {
                                setList(etThingCode.getText().toString().trim(), etThingId.getText().toString().trim());
                                freeUI_generalFragmentDialog.dismiss();
                            } else {
                                etThingId.requestFocus();
                            }
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
                etThingId.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (start == 0 && before == 0 && count > 1) {
                            if (etThingCode.getText().toString().trim().length() > 0) {
                                setList(etThingCode.getText().toString().trim(), etThingId.getText().toString().trim());
                                freeUI_generalFragmentDialog.dismiss();
                            } else {
                                etThingCode.requestFocus();
                            }
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        freeUI_generalFragmentDialog.dismiss();
                    }
                });
                freeUI_generalFragmentDialog.setLayout(view);
                freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "11");
                break;
            case R.id.rlCarryId:
                addCarryIdDialog = FreeApi_DialogUtils.getScanDialog(this, "请扫描载具编号", new FreeUI_TextCallback() {
                    @Override
                    public void onSuccess(String s) {
                        String carryIds = tvCarryId.getText().toString().trim();
                        boolean isSame = false;
                        if (carryIds.length() == 0) {
                            tvCarryId.setText(s);
                        } else {
                            if (carryIds.contains(";")) {
                                String[] array = carryIds.split(";");
                                for (String str : array) {
                                    if (str.equals(s)) {
                                        isSame = true;
                                    }
                                }
                            } else {
                                if (carryIds.equals(s)) {
                                    isSame = true;
                                }
                            }
                            if (isSame) {
                                showToast("载具编号不能重复", false);
                            } else {
                                StringBuffer stringBuffer = new StringBuffer();
                                stringBuffer.append(carryIds);
                                stringBuffer.append(";");
                                stringBuffer.append(s);
                                tvCarryId.setText(stringBuffer.toString());
                            }
                        }
                        addCarryIdDialog.dismiss();
                    }
                });
                addCarryIdDialog.show(getSupportFragmentManager(), "addCarryIdDialog");
                break;
        }
    }

    private void setList(String matCode, String matLotId) {
        String seq = String.valueOf(mLots.size() + 1);
        mLots.add(new ThingBean(seq, matCode, matLotId));
        thingListAdapter.notifyDataSetChanged();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_start_work;
    }

    @Override
    protected void initView() {
        rvThingList.setLayoutManager(new LinearLayoutManager(StartWorkActivity.this));
        rvThingList.setItemAnimator(new DefaultItemAnimator());
        rvThingList.addItemDecoration(new DividerItemDecoration(StartWorkActivity.this, 1));

        thingListAdapter = new ThingListAdapter(mLots);
        thingListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        thingListAdapter.isFirstOnly(true);
        rvThingList.setAdapter(thingListAdapter);
    }

    @Override
    protected void initData() {
        tvUser.setText(Constant.CURRENT_USER);
        etLotId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    tvStep.setText("T-STEP_101");
                    tvLotNum.setText("20");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("开始作业");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast("开始作业成功", true);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        StartWorkActivity.this.finish();
                    }
                }, 2000);
            }
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }
}
